﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAL;
using DTO;
using Utility;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class Admin_ThemSanPham : System.Web.UI.Page
{
    private SqlConnection cn = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
        cn = new SqlConnection(stringConnection);
        cn.Open();
        LoadData();
    }
    private void LoadData()
    {
        LoaiCollection Loaicollection =
             LoaiBUS.GETALL();
        ddl_loaisp.DataSource = Loaicollection;
        ddl_loaisp.DataTextField = "TenLoai";
        ddl_loaisp.DataValueField = "MaLoai";
        ddl_loaisp.DataBind();
        NhaSanXuatCollection NSXcollection =
            NhaSanXuatBUS.GETALL();
        ddl_nhasx.DataSource = NSXcollection;
        ddl_nhasx.DataTextField = "TenNhaSanxuat";
        ddl_nhasx.DataValueField = "MaNhaSanXuat";
        ddl_nhasx.DataBind();

    }
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        SanPham sp = new SanPham();
        sp.TenSanPham = txt_tensp.Text;
        sp.MaLoai = int.Parse(ddl_loaisp.SelectedValue);
        sp.CPU = txt_CPU.Text;
        sp.RAM = txt_ram.Text;
        sp.HDD = txt_hdd.Text;
        sp.CardManHinh = txt_cardmanhinh.Text;
        sp.CD_DVD = txt_CD.Text;
        sp.ManHinh = txt_manhinh.Text;
        sp.Mang = txt_mang.Text;
        sp.TichHop = txt_tichhop.Text;
        sp.TrongLuong = txt_trongluong.Text;
        sp.Pin = txt_pin.Text;
        sp.HeDieuHanh = txt_hdh.Text;
        sp.ThoiGianBaoHanh = txt_tgia_bhanh.Text;
        sp.KhuyenMai = txt_khuyenmai.Text;
        sp.Gia = int.Parse(txt_gia.Text);
        sp.MaNhaSanXuat = int.Parse(ddl_nhasx.SelectedValue.ToString());
        SanPhamBUS.INSERT(sp);

        HttpFileCollection httpFileCollection = Request.Files;
        for (int i = 0; i < httpFileCollection.Count; i++)
        {

            HttpPostedFile httpPostedFile = httpFileCollection[i];
            if (httpPostedFile.ContentLength > 0)
            {


                HinhAnh hinhanh = new HinhAnh();
                string t = Session["TenDangNhap_NhanVien"].ToString();

                SqlCommand laymasp = new SqlCommand("select MaSanPham from SANPHAM where TenDangNhap_NhanVien=@tendangnhap and MaSanPham = (select MAX(MaSanPham) from SANPHAM)", cn);
                laymasp.Parameters.AddWithValue("@tendangnhap", t);
                hinhanh.MaSanPham = int.Parse(laymasp.ExecuteScalar().ToString());

                string filename = DateTime.Now.Ticks.ToString() + "_" + httpPostedFile.FileName;
                httpPostedFile.SaveAs(Server.MapPath("../Photos/") + filename);
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("../Photos/") + filename);
                ImageUtility.MakeThumbnailPhoto(img, 100, 110, Server.MapPath("../Photos/thumbs/"), filename);
                hinhanh.DuongDan = filename;
                HinhAnhBUS.INSERT(hinhanh);
            }
        }

        Response.Redirect("~/Admin/DanhSachSanPham.aspx");
    }
}